<?php
/**
 * Author Yulin
 * Func   所有彩种
 */
defined('IN_YuLin') || exit('NO PERMIT!');
$seo['title'] = '大众彩票-活动中心';
$pageTitle    = '活动中心';
$navTag       = 'activity';

// ================= 替换文本信息准备 ============= 
// 当前用户等级
$jinJiLevel_str   = '未登录';
// 可领取奖励
$jinJiReward_str  = '0';
// 领取通道按钮
$rewardLingqu_str = '<a class="disable">不可领取</a>';
// 所有等级设置晋级图
$levelStr         = '';
// 昨日投注数额
$yesterdayBetAmount = '0.00';

$levelTab         = Table('user_level');
$sql              = 'SELECT id, level, title, group_point, reward, reward FROM ' . $levelTab . ' WHERE status = 1 ORDER BY group_point ASC';
$levelData = $db->getall($sql);
$levelIdKey= array();
foreach ($levelData as $k => $v){
    $levelIdKey[$v['id']] = $v;
    $levelStr .= '<tr><td>' . $v['level'] . '</td><td>' . $v['group_point'] . '</td><td>' . $v['reward'] . '</td><td>' . $v['reward'] . '</td></tr>';
}
// 默认展示页面
$page         = 'activity/index';
$mainTab      = Table('lottery_order_main');
$actTab       = Table('activity');
$aid          = NoBadStr($_GET['aid']);

if ($_user){
    // 如果有登录， 设置活动页面上登录状态， 等级，以及晋级奖励的领取状态
    $jinJiLevel_str   = $levelIdKey[$_userInfo['level_id']]['level'];
    if ($levelIdKey[$_userInfo['level_id']]['reward'] != 0 && $_userInfo['level_id'] != $_userInfo['last_take_prize_level']){
        // 如果当前等级有奖励设置（reward）并且上次领取的等级不等于当前等级， 则表示有奖励可以领取
        $last_take_level = $_userInfo['last_take_prize_level'] ? $_userInfo['last_take_prize_level'] : 0;
        foreach ($levelIdKey as $k => $v){
            if ($k > $last_take_level && $last_take_level < $_userInfo['level_id']){
                $last_take_level = $k;
                $jinJiReward_str += $v['reward'];
            }
        }
        $rewardLingqu_str = '<a class="" href="?m=index&c=activity&a=reward">领取奖励</a>';
    }
    $sql = 'SELECT SUM(amount) as y_amount FROM ' . $mainTab . ' WHERE createtime > ' . strtotime(date('Ymd', strtotime('-1 day'))) . ' AND createtime < ' . strtotime(date('Ymd'));
    $orderAmountData = $db->getrow($sql);
    if($orderAmountData['y_amount'] > 0){
        $yesterdayBetAmount = $orderAmountData['y_amount'];
    }
}
if ($aid){
    $page     = 'activity/more';
    $actInfo = $db->getrow('SELECT * FROM ' . $actTab . ' WHERE id = ' . $aid);
    // 替换关键字符信息
    if (strpos($actInfo['contents'], '[LEVEL_RULE]') !== false){
        $actInfo['contents'] = str_replace('[LEVEL_RULE]', $levelStr, $actInfo['contents']);
    }
    if (strpos($actInfo['contents'], '[USER_LEVEL]') !== false){
        $actInfo['contents'] = str_replace('[USER_LEVEL]', $jinJiLevel_str, $actInfo['contents']);
    }
    if (strpos($actInfo['contents'], '[LEVEL_REWARD]') !== false){
        $actInfo['contents'] = str_replace('[LEVEL_REWARD]', $jinJiReward_str, $actInfo['contents']);
    }
    if (strpos($actInfo['contents'], '[TAKE_STATUS]') !== false){
        $actInfo['contents'] = str_replace('[TAKE_STATUS]', $rewardLingqu_str, $actInfo['contents']);
    }
    if (strpos($actInfo['contents'], '[LAST_DAY_BET_AMOUNT]') !== false){
        $actInfo['contents'] = str_replace('[LAST_DAY_BET_AMOUNT]', $yesterdayBetAmount, $actInfo['contents']);
    }
    $tpl->display($page);
    die();
}

$actSql       = 'SELECT id, title, `desc` FROM ' . $actTab . ' WHERE source = 2 AND status = 1';
$actList      = $db->getall($actSql);
$tpl->display($page);